package com.mflex.shelflocationquery.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mflex.shelflocationquery.R;
import com.mflex.shelflocationquery.adapter.TxnListAdapter;
import com.mflex.shelflocationquery.model.ResponseError;
import com.mflex.shelflocationquery.utils.MyCLient;
import com.mflex.shelflocationquery.utils.XToastUtils;
import com.xuexiang.xhttp2.reflect.TypeToken;
import com.xuexiang.xui.utils.CollectionUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.StringEntity;

public class MainActivity extends AppCompatActivity {
    private EditText stvBatchId;
    private EditText stvOkQty;
    private EditText stvNgQty;
//    ListView lvTxnList;
    MiniLoadingDialog mMiniLoadingDialog;

//    private Button buttonSave;
    private Button buttonClear;

//    List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stvBatchId = (EditText) findViewById(R.id.stvBatchId);
        stvOkQty = (EditText) findViewById(R.id.stvOkQty);
        stvNgQty = (EditText) findViewById(R.id.stvNgQty);
//        buttonSave = (Button) findViewById(R.id.buttonSave);
        buttonClear = (Button) findViewById(R.id.buttonclean);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载中...");

        stvOkQty.setInputType(InputType.TYPE_NULL);
        stvNgQty.setInputType(InputType.TYPE_NULL);

//        list =  new ArrayList<>();
//        lvTxnList = (ListView) findViewById(R.id.lvTxnList);

        stvBatchId.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && (keyCode == 66 || keyCode == 61)) {
                    if (!TextUtils.isEmpty(stvBatchId.getText().toString().trim())) {
                        mMiniLoadingDialog.show();
//                        XHttp.get("api/CheckPackingDatas/shelflocationpcs")
//                                .params("shelflocation", stvBatchId.getText().toString())
//                                .syncRequest(false)
//                                .onMainThread(true)
//                                .timeOut(120000)
//                                .execute(new SimpleCallBack<List<String>>() {
//                                    @Override
//                                    public void onSuccess(List<String> response) {
//
//                                        mMiniLoadingDialog.dismiss();
//
//
//
//                                    }
//
//                                    @Override
//                                    public void onError(ApiException e) {
//                                        mMiniLoadingDialog.dismiss();
//                                        new MaterialDialog.Builder(MainActivity.this)
//                                                .iconRes(R.drawable.icon_warning)
//                                                .title(R.string.tip_warning)
//                                                .content(e.getMessage())
//                                                .positiveText(R.string.lab_submit)
//                                                .show();
//                                    }
//                                });

                        RequestParams params = new RequestParams();
                        params.put("shelflocation", stvBatchId.getText().toString());
                        MyCLient.get("api/CheckPackingDatas/shelflocationpcs", params, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int i, Header[] headers, byte[] bytes) {
//                                mMiniLoadingDialog.dismiss();
                                Gson gson = new Gson();
                                List<String> list = gson.fromJson(new String(bytes), new TypeToken<List<String>>() {
                                }.getType());
                                 if(CollectionUtils.isEmpty(list)){
                                     XToastUtils.error("未查询到保干箱数据");
                                     return;
                                 }
                                query(list);
//                                TxnListAdapter txnListAdapter = new TxnListAdapter(MainActivity.this, list);
//                                lvTxnList.setAdapter(txnListAdapter);
                                stvBatchId.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        stvBatchId.requestFocus();
                                    }
                                },200);
                            }

                            @Override
                            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                                mMiniLoadingDialog.dismiss();
                                XToastUtils.error(throwable.getMessage());
                            }
                        });
                    }
                }
                return false;
            }
        });
        stvBatchId.postDelayed(new Runnable() {
            @Override
            public void run() {
                stvBatchId.requestFocus();
            }
        },200);

//        buttonSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mMiniLoadingDialog.show();
//                Gson gson = new Gson();
//                String json = gson.toJson(list);
//                StringEntity entity = new StringEntity(json, "UTF-8");
//                entity.setContentType("application/json");
//
//                MyCLient.post(MainActivity.this, "api/CheckPackingDatas/shelflocationvalidation", entity, new AsyncHttpResponseHandler() {
//                    @Override
//                    public void onSuccess(int i, Header[] headers, byte[] bytes) {
//                        mMiniLoadingDialog.dismiss();
//                        Gson gson = new Gson();
//                        ResponseError res = gson.fromJson(new String(bytes), new TypeToken<ResponseError>() {
//                        }.getType());
//                        TxnListAdapter txnListAdapter = new TxnListAdapter(MainActivity.this, new ArrayList<>());
//                        lvTxnList.setAdapter(txnListAdapter);
//                        stvOkQty.setText(String.valueOf(res.getOkQty()));
//                        stvNgQty.setText(String.valueOf(res.getNgQty()));
//                        // 处理响应
//                    }
//                    @Override
//                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
//                        mMiniLoadingDialog.dismiss();
//                        XToastUtils.error(throwable.getMessage());
//                    }
//                });
//            }
//        });

        buttonClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stvBatchId.setText("");
                stvOkQty.setText("");
                stvNgQty.setText("");
                stvBatchId.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        stvBatchId.requestFocus();
                    }
                },200);
            }
        });

    }

    private void query(List<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        StringEntity entity = new StringEntity(json, "UTF-8");
        entity.setContentType("application/json");

        MyCLient.post(MainActivity.this, "api/CheckPackingDatas/shelflocationvalidation", entity, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                mMiniLoadingDialog.dismiss();
                Gson gson = new Gson();
                ResponseError res = gson.fromJson(new String(bytes), new TypeToken<ResponseError>() {
                }.getType());
//                TxnListAdapter txnListAdapter = new TxnListAdapter(MainActivity.this, new ArrayList<>());
//                lvTxnList.setAdapter(txnListAdapter);
                stvOkQty.setText(String.valueOf(res.getOkQty()));
                stvNgQty.setText(String.valueOf(res.getNgQty()));
                // 处理响应
                stvBatchId.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        stvBatchId.requestFocus();
                    }
                },200);
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                mMiniLoadingDialog.dismiss();
                XToastUtils.error(throwable.getMessage());
            }
        });
    }

}